<template>
    <el-form :model="score1" label-position="right" label-width="auto">
        <el-row :gutter="10">
            <el-col :span="12">
                <el-form-item label="商家名" prop="name">
                    <el-input v-model="orderMessage.shop_name" placeholder="商家名" clearable disabled/>
                </el-form-item>
            </el-col>
            <div class="demo-rate-block">
                <span class="demonstration">商家评分</span>
                <el-rate v-model="score1.score" />
            </div>
        </el-row>
        <el-row :gutter="10">
            <el-col :span="12">
                <el-form-item label="骑手名" prop="name">
                    <el-input v-model="orderMessage.rider_name" placeholder="骑手名" clearable disabled/>
                </el-form-item>
            </el-col>
            <div class="demo-rate-block">
                <span class="demonstration">骑手评分</span>
                <el-rate v-model="score2.score" />
            </div>
        </el-row>
    </el-form>
    <el-row>
        <el-col>
            <div style="text-align: center;">
                <el-button type="primary" @click="doSave1">保存</el-button>
                <el-button @click="doCancel">取消</el-button>
            </div>
        </el-col>
    </el-row>
</template>

<script setup lang="ts">
import {inject, ref} from 'vue'
import {ElMessage} from "element-plus";
import axios from 'axios';

//---------------------------- 自定义组件事件 -----------------------------
const emits = defineEmits([
    "closeDialog",	// 关闭 dialog
    "flushPage"		// 刷新页面
]);
//评价商家
const score1 = ref({
    dingdan_num: "",//评分的订单编号
    scorer_id: 0,//评分人id
    scorer_type: 0,//评分人类型
    bescorer_id: 0,//被评分人id
    bescorer_type: 1,//被评分人类型
    score: 0,//评分
})
//评价骑手
const score2 = ref({
    dingdan_num: "",//评分的订单编号
    scorer_id: 0,//评分人id
    scorer_type: 0,//评分人类型
    bescorer_id: 0,//被评分人id
    bescorer_type: 2,//被评分人类型
    score: 0,//评分
})
const orderMessage=ref({
    id:'',//id
    dingdan_num: '',//订单编号
    user_id: 0,//用户编号
    shop_name: 0,//店铺名字
    rider_name: 0,//骑手名字
    rider_id: 0,//骑手编号
    shop_id: 0,//店铺编号
});
//--------------------------获取用户信息-----------------------
const user=ref({});
const acquireUsers=async ()=>{
    const {data} = await axios.get('/api/user1',{params:user.value});
    user.value=data;
    GetDingdan();
    score1.value.scorer_id = user.value.id;
    score2.value.scorer_id = user.value.id;

}
//--------------------
const handleUpdateData = (obj) => {
    orderMessage.value = obj;
    score1.value.dingdan_num = orderMessage.value.dingdan_num;
    score2.value.dingdan_num = orderMessage.value.dingdan_num;
    diandan.value.dingdan_num = orderMessage.value.dingdan_num;
    acquireUsers();
}
//获取订单信息
const diandan = ref({
    id: "",//id
    dingdan_num: "",//评分的订单编号
    shop_id: 0,//商店id
    rider_id: 0,//骑手id
});
const GetDingdan=async ()=>{
    const {data} = await axios.get('/api/dingdan/score',{params:diandan.value});
    diandan.value=data;
    console.log(orderMessage.value);
    score1.value.bescorer_id = diandan.value.shop_id;
    score2.value.bescorer_id = diandan.value.rider_id;
}
// 暴露给父组件使用的方法
defineExpose({
    handleUpdateData,
})

// 取消
const doCancel = () => {
    emits("closeDialog");
}

// 更新
const doSave1 = async () => {
    console.log(score1.value);
    const {data} = await axios.post('/api/score',score1.value);
    console.log(data);
    doSave2();
    updateShopScore();
    updateRiderScore();
}
const doSave2 = async () => {
    const {data} = await axios.post('/api/score', score2.value);
    if (data > 0) {
        emits("flushPage");				// 触发事件： 刷新页面
        emits("closeDialog");			// 触发事件：关闭 dialog
        ElMessage.success("评价成功！");
    } else {
        ElMessage.error("评价失败！");
    }
}
const updateShopScore = async () => {
    const {data} = await axios.put('/api/shop/shopScore/'+diandan.value.shop_id);
    if (data > 0) {
        ElMessage.success("您的评价是我们进步的动力！");
    } else {
        ElMessage.error("更新失败！");
    }

}

const updateRiderScore = async () => {
    const {data} = await axios.put('/api/rider/score/'+diandan.value.rider_id);
}
</script>

<style scoped>
</style>